package com.bytech.admin.biz.controller.emoticon;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytech.common.config.UploadConfig;
import com.bytech.common.biz.controller.base.BasicController;
import com.bytech.common.biz.entity.base.BaseResult;
import com.bytech.common.biz.entity.emoticon.Emoticon;
import com.bytech.common.biz.entity.emoticon.EmoticonDto;
import com.bytech.common.biz.entity.emoticon.EmoticonQuery;
import com.bytech.common.biz.enums.UploadResourceTypeEnum;
import com.bytech.common.biz.enums.emoticon.EmoticonSourceEnum;
import com.bytech.common.biz.service.remote.comment.RemoteCommentService;
import com.bytech.common.biz.thirdparty.gallery.service.GalleryApiClient;
import com.bytech.common.utils.StringUtils;
import com.google.common.collect.Lists;

/**
 * @Author Zhao
 * @Create 2021/11/26 9:52
 */
@Controller
@RequestMapping("/emoticon")
public class EmoticonController extends BasicController {

    @Resource
    private RemoteCommentService remoteCommentService;
    @Resource
    private UploadConfig uploadConfig;
    @Resource
    private GalleryApiClient galleryApiClient;

    @GetMapping("/list")
    public String toEmoticonList(Model model) {
        model.addAttribute("emoticonFroms", EmoticonSourceEnum.values());
        return "/emoticon/emoticons";
    }

    @PostMapping("/page")
    @ResponseBody
    public BaseResult<Page<EmoticonDto>> pageEmoticon(EmoticonQuery<Emoticon> query) {
        return BaseResult.<Page<EmoticonDto>>success().data(remoteCommentService.pageEmoticonByQuery(query)).build();
    }

    @PostMapping("/delete")
    @ResponseBody
    public BaseResult<?> doDeleteEmoticon(Long id) {
        return remoteCommentService.doDeleteEmoticonById(id);
    }

    @PostMapping("/toggleUsable")
    @ResponseBody
    public BaseResult<?> doToggleEmoticonUsable(Long id) {
        return remoteCommentService.doToggleEmoticonUsable(id);
    }

    @PostMapping("/modifyName")
    @ResponseBody
    public BaseResult<?> doModifyEmoticonName(Long id, String name) {
        if (StringUtils.isBlank(name)) {
            return BaseResult.error("请设置名称").build();
        }
        return remoteCommentService.doModifyEmoticonName(id, name);
    }

    @PostMapping("/doUpload")
    @ResponseBody
    public BaseResult<?> doUploadEmoticon(MultipartFile[] files, String emoticonCode) {
        return galleryApiClient.doUploadImages(Arrays.asList(files), UploadResourceTypeEnum.EMOTICON, (resultUrlList) -> {
            List<Emoticon> emoticonList = Lists.newArrayList();
            resultUrlList.forEach(url -> {
                Emoticon emoticon = new Emoticon();
                emoticon.setImgUrl(url);
                emoticon.setIsUsable(true);
                emoticon.setEmoticonFrom(EmoticonSourceEnum.of(emoticonCode).getCode());
                emoticon.setCreateTime(new Date());
                emoticon.setModifyTime(new Date());
                emoticonList.add(emoticon);
            });
            remoteCommentService.doBatchAddEmoticon(emoticonList);
            return BaseResult.success("上传成功！").build();
        });
    }
}
